/**************************************************
 * Filename: LogicThread.java
 * Version: v1.0
 * CreatedDate: 2011-11-27
 * Copyright (C) 2011 By cafebabe.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * If you would like to negotiate alternate licensing terms, you may do
 * so by contacting the author: talentyao@foxmail.com
 ***************************************************/

package com.train;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.swing.JOptionPane;

import com.train.gui.RobTicket;

/**
 * 功能描述
 * 
 * @author cafebabe
 * @since 2011-11-27
 * @version 1.0
 */
public class LogicThread extends Thread {

	private UserInfo ui;
	private TrainClient client;
	private boolean isEnd = false;
	private RobTicket rob;

	/**
	 * 构造函数
	 * @param robTicket 
	 */
	public LogicThread(UserInfo ui, TrainClient client, RobTicket rob) {
		this.ui = ui;
		this.client = client;
		this.rob = rob;
	}

	/**
	 * override 方法
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		while (!isEnd) {
			Result rs = new Result();
			String randCode = null;
			rob.getTextArea().append(Util.formatInfo("正在查询"));
			List<TrainQueryInfo> allTrain = client.queryTrain(ui.getFromCity(),
					ui.getToCity(), ui.getStartDate(), ui.getRangDate());
			if (allTrain.size() == 0) {
				rs.setState(false);
				rs.setMsg("未找到从" + ui.getFromCity() + "到" + ui.getToCity()
						+ "的可预订列车信息");
				rob.getTextArea().append(Util.formatInfo(rs.toString()));
				JOptionPane.showMessageDialog(null, "请从新输入列车信息");
				rob.getBtnSORE().setText("开始");				
				return;
			} else {
				rs.setState(true);
				Collections.sort(allTrain, new TrainComparator());
				for (TrainQueryInfo ti : allTrain) {
					System.out.println(ti);
				}
				rs.setMsg(ui.getStartDate() + "共找到从" + ui.getFromCity() + "到"
						+ ui.getToCity() + "的可预订列车" + allTrain.size() + "趟");
				rob.getTextArea().append(Util.formatInfo(rs.toString()));
			}
			TrainQueryInfo train = allTrain.get(0);
			ui.setSeatType(Util.getSeatAI(train));
			rob.getTextArea().append(Util.formatInfo("查询历史订单"));
			rs = client.queryOrder();
			System.out.println(rs);

			System.out.println("开始预定");
			rs = client.book(ui.getRangDate(), ui.getStartDate(), train);
			System.out.println(rs);
			try {
				randCode = client.getCode(Constant.ORDER_CODE_URL);
			} catch (IOException e) {
				e.printStackTrace();
			}
			String token = client.getToken();
			rob.getTextArea().append(Util.formatInfo("提交订单"));
			rs = client.submiOrder(randCode, token, ui, train);
			System.out.println(rs);
			rob.getTextArea().append(Util.formatInfo("查询订单"));
			rs = client.queryOrder();
			System.out.println(rs);
		}
	}

	/**
	 * @return Returns the ui.
	 */
	public UserInfo getUi() {
		return ui;
	}

	/**
	 * @param ui The ui to set.
	 */
	public void setUi(UserInfo ui) {
		this.ui = ui;
	}

	/**
	 * @return Returns the client.
	 */
	public TrainClient getClient() {
		return client;
	}

	/**
	 * @param client The client to set.
	 */
	public void setClient(TrainClient client) {
		this.client = client;
	}

	/**
	 * @param isEnd The isEnd to set.
	 */
	public void setEnd(boolean isEnd) {
		this.isEnd = isEnd;
	}
}
